package com.me.controller;

import com.me.pojo.FileMail;
import com.me.pojo.MailInfo;
import com.me.pojo.SimpleMail;
import com.me.service.AsyncTaskInfo;
import com.me.service.Mail.MailInfoService;
import com.me.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/mail")
public class SendMailController {

    @Autowired
    SendMailService mailService;

    @Autowired
    AsyncTaskInfo asyncTaskInfo;

    @RequestMapping("/mailView")
    public String mailView() {
        return "mail/sendMail";
    }

    @RequestMapping("/fileMailView")
    public String fileMailView() {
        return "mail/fileMailView";
    }

    @RequestMapping("/snedMail")
    public String sendMail(SimpleMail mail, Model model) {
        mailService.sendMail(mail.getSubject(),mail.getText(),mail.getTo(),mail.getFrom());
        asyncTaskInfo.mailInfo(asyncTaskInfo.simpleMailInfo(mail));
        model.addAttribute("msg","发送成功");
        return "mail/sendMail";
    }

    SimpleDateFormat sdf = new SimpleDateFormat("/yyy/MM/dd");

    @RequestMapping("/fileMail")
    public String sendMailFile(FileMail fileMail, Model model, HttpServletRequest req) {
        mailService.sendMailFile(fileMail);
        //asyncTaskInfo.mailInfo(asyncTaskInfo.fileMailInfo(fileMail));
        model.addAttribute("msg","发送成功");
        return "mail/fileMailView";
    }

}
